package com.cfjjc.gz.common.filter;

import java.io.IOException;
import java.util.StringTokenizer;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.usraccount.vo.A_USRVO;

public class CheckLoginFilter implements Filter {
	
	protected final Log logger = LogFactory.getLog(getClass());

	FilterConfig fc = null;

	// 不处理列表
	String noFilterList = null;

	// 用于web中配置session名
	String session_user = null;

	// 校验失败后页面
	String failureAction = null;

	public void destroy() {
		this.fc = null;
		this.noFilterList = null;
		this.session_user = null;
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();
		
		logger.debug("**********doFilter***********");

		if (session == null) {
			response.sendRedirect(failureAction);
			return;
		}
		
		//String path = request..getServletPath();
		String url = request.getRequestURI();
		String path = url.substring((request.getContextPath()).length());
		A_USRVO user = (A_USRVO) session.getAttribute(session_user);
		if (user == null) {
			// 当前请求路径需要进行拦截过滤，则提示用户重新登录
			if (noFilterList != null && !"".equals(noFilterList)) {
				if (!isNoFilterPath(path)) {
					response.sendRedirect(failureAction);
					return;
				}
			} 
			
		} else {
			if (SystemConst.YES_CONST.equals(user.getFirst_login())) {
				if (!path.equals("/showMustChangePass.do")
						&& !path.equals("/showChangePass.do")
						&& !path.equals("/doChangePass.do")) {
					response.sendRedirect("showMustChangePass.do");
				    return;
				}
			}
		}
		chain.doFilter(request, response);
	}

	/**
	 * 判断当前的请求路径是否为不需要进行拦截过滤的路径。
	 * @param path request path
	 * @return true：是不需要进行拦截过滤的路径；false：需要进行拦截过滤的路径
	 */
	protected boolean isNoFilterPath(String path) {
		boolean noFilterflag = false;
		String temp = null;
		StringTokenizer tokens = new StringTokenizer(noFilterList, ",");
		int countPath = tokens.countTokens();
		for (int i = 0; i < countPath; i++) {
			temp = (String) tokens.nextElement();
			if (temp.equalsIgnoreCase(path)) {
				noFilterflag = true;
				break;
			}
		}

		return noFilterflag;
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.fc = filterConfig;
		this.session_user = fc.getInitParameter("session_user");
		this.noFilterList = fc.getInitParameter("noFilterList");
		this.failureAction = fc.getInitParameter("failureAction");
	}

}
